<template>
    <div class="pdf-viewer">
      <!-- 左边显示 PDF 预览 -->
      <h3>授权证书</h3>
      <div class="pdf-preview">
        <pdf :src="pdfSrc" @error="onError" @num-pages="onNumPages"></pdf>
      </div>
  
      <!-- 右边显示下载按钮 -->
      <div class="download-button">
        <button @click="downloadPdf">点击下载该授权证书</button>
      </div>
    </div>
  </template>
  
  <script>
  import pdf from 'vue-pdf';
  
  export default {
    components: { pdf },
    data() {
      return {
        pdfSrc: pdf.createLoadingTask('@/assets/404_images/PDF.pdf'), // 使用 createLoadingTask 来处理 PDF 文件
      };
    },
    methods: {
      onError(error) {
        console.error('加载 PDF 出错：', error);
      },
      onNumPages(pages) {
        console.log(`PDF 总页数：${pages}`);
      },
      downloadPdf() {
        fetch(this.pdfSrc.url)
          .then(response => response.blob())
          .then(blob => {
            const blobUrl = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = blobUrl;
            link.download = 'certificate.pdf';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            window.URL.revokeObjectURL(blobUrl);
          })
          .catch(e => console.error('Error downloading the PDF:', e));
      }
    }
  };
  </script>
  
  <style scoped>
  .pdf-viewer {
    display: flex;
    justify-content: space-between;
  }
  
  .pdf-preview {
    background-color: antiquewhite;
    width: 40%;
  }
  
  .download-button {
    padding-top: 20px;
  }
  </style>
  